<?php
class Edit extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/group/'));
		
		$uid = $this->get('uid', 'int');
		if(!$uid) header('Location:/rbac/user/');
		
		$data = array();
		$tpl = 'rbac/user/userinfo.html';
		
		//获取用户信息
		$data['info'] = $mUser->userInfoByUid($uid);
		
		$data['act'] = 'edit';
		$this->display($data, $tpl);
	}
	public function json() {
		$uid = $this->post('uid', 'str');
		$username = $this->post('username', 'str');
		$realname = $this->post('realname', 'str');
		$password = $this->post('password', 'str');
		$password1 = $this->post('password1', 'str');
		
		if(!$uid) $this->displayJSON(array('status' => -1, 'message' => '未知用户UID'));
		if(!$username) $this->displayJSON(array('status' => -2, 'message' => '请输入登录名'));
		if(!$realname) $this->displayJSON(array('status' => -3, 'message' => '请输入真实姓名'));
		if(!$password) $this->displayJSON(array('status' => -4, 'message' => '请输入密码'));
		if($password != $password1) $this->displayJSON(array('status' => -5, 'message' => '两次输入密码不一致'));
		
		$mUser = new User_model();
		//检查登录名
		$params = array('username' => $username, 'uid !=' => $uid);
		$result = $mUser->userInfo($params);
		if($result) $this->displayJSON(array('status' => -6, 'message' => '登录名已存在'));
		
		$cols = array('username' => $username, 'realname' => $realname, 'password' => $password);
		$result = $mUser->editUser($uid, $cols);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '修改用户成功'));
		else $this->displayJSON(array('status' => -7, 'message' => '修改用户失败'));
	}
}